package com.sinosoft.dsp.platform.components.portal.input;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinosoft.builder.templates.concept.ControlType;
import com.sinosoft.builder.templates.concept.PresentationType;
import com.sinosoft.builder.templates.concept.SqlConfigType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.portal.input.elements.ISinoTagElement;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.utils.Const;

public class SinoDefaultSearchBean {

	private static final Log log = Log.getLog(SinoDefaultSearchBean.class
			.getName());

	private SinoSearchFormViewBean form;

	private PresentationType meta;

	// 条件的行
	private List<SinoDefaultSearchConditionBean[]> conditionRows;

	private ISinoSearchFormDataViewBean datas;

	private SinoDefaultSearchControlBean[] buttons;

	public void setForm(SinoSearchFormViewBean form) {
		this.form = form;
	}

	public SinoSearchFormViewBean getForm() {
		return form;
	}

	public PresentationType getMeta() {
		return meta;
	}

	public SinoDefaultSearchBean setMeta(PresentationType meta) {
		this.meta = meta;
		return this;
	}

	public void flushSearchs() {
		conditionRows = null;
	}

	public void flushButtons() {
		buttons = null;
	}

	public List<SinoDefaultSearchConditionBean[]> getConditionRows()
			throws SinoPlatformException {
		if (conditionRows == null) {
			initSearchConditionRows();
		}
		return conditionRows;
	}

	public Map<String, ISinoTagElement> getConditionFieldsMap()
			throws SinoPlatformException {
		Map<String, ISinoTagElement> fields = new HashMap<String, ISinoTagElement>();
		List<SinoDefaultSearchConditionBean[]> list = getConditionRows();
		if (list != null) {
			for (SinoDefaultSearchConditionBean[] row : list) {
				for (SinoDefaultSearchConditionBean bean : row) {
					if (bean != null && bean.getValue() != null) {
						fields.put(bean.getElementId(), bean.getValue());
					}
				}
			}
		}
		return fields;
	}

	public ISinoSearchFormDataViewBean getDatas() {
		return datas;
	}

	public void initDatas(SqlConfigType sqlConfig) {
		try {
			datas = new SinoSearchFormDataViewBean(sqlConfig, this);
			datas.init(null);
		} catch (SinoException e) {
			log.error("创建查询表单数据ViewBean出现错误");
		}
	}

	public SinoDefaultSearchControlBean[] getButtons()
			throws SinoPlatformException {
		if (buttons == null) {
			initSearchConditionControls();
		}
		return buttons;
	}

	public String getLabelStyle() {
		return "width:" + (int) (100 / getColumnNum() / 4) + "%;";
	}

	public String getContentStyle() {
		return "width:" + (int) (100 * 3 / getColumnNum() / 4) + "%;";
	}

	public int getColumnNum() {
		return meta.getSearchCondition().getColumnNum();
	}

	private void initSearchConditionControls() throws SinoPlatformException {
		// 初始化控制按钮
		ControlType controlType = meta.getControl();
		List<SinoDefaultSearchControlBean> btns = new ArrayList<SinoDefaultSearchControlBean>();
		if (controlType != null && controlType.getTagElement() != null) {
			for (TagElementType tag : controlType.getTagElement()) {
				btns.add(form.getControl(tag.getId()));
			}
		}
		this.buttons = btns.toArray(new SinoDefaultSearchControlBean[btns
				.size()]);
	}

	private void initSearchConditionRows() throws SinoPlatformException {
		List<SinoDefaultSearchConditionBean[]> rows = null;
		if (meta != null && meta.getSearchCondition() != null) {
			int columnNum = getColumnNum();
			rows = new ArrayList<SinoDefaultSearchConditionBean[]>();
			String sequence = meta.getSearchCondition().getSequence();
			if (!Const.isEmpty(sequence)) {
				String[] ids = sequence.split(",");
				int index = 0;
				List<SinoDefaultSearchConditionBean> conditionRows = new ArrayList<SinoDefaultSearchConditionBean>();
				SinoDefaultSearchConditionBean condition = null;
				for (String id : ids) {
					if (!Const.isEmpty(id)) {

						condition = form
								.getCondition(SinoSearchFormConditionType
										.getId(id));
						if (condition == null) {
							throw new SinoPlatformException("序列中的ID与条件定义不匹配，ID"
									+ id);
						}

						if (!condition.isDisplay()) {
							continue;
						}

						SinoSearchFormConditionType type = SinoSearchFormConditionType
								.getType(id);

						condition.resetSpan();

						// 单列
						if (type == SinoSearchFormConditionType.SINGLE) {
							if (index > 0) {
								addRow(rows, conditionRows, columnNum);
							}
							condition.setColspan(columnNum * 2 - 1);
							conditionRows.add(condition);
							index = columnNum * 2;
						}
						// 单列所有列
						else if (type == SinoSearchFormConditionType.SINGLE_ALL) {
							if (index > 0) {
								addRow(rows, conditionRows, columnNum);
							}
							condition.setColspan(columnNum * 2);
							condition.setFullColumn(true);
							conditionRows.add(condition);
							index = columnNum * 2;
						}
						// 自然单列
						else if (type == SinoSearchFormConditionType.SINGLE_NORMAL) {
							// 如果小于2换行
							if ((columnNum * 2 - index) < 2) {
								addRow(rows, conditionRows, columnNum);
							}
							condition.setColspan(columnNum * 2 - index - 1);
							conditionRows.add(condition);
							index = columnNum * 2;
						}
						// 如果是换行
						else if (type == SinoSearchFormConditionType.BREAK) {
							if (index > 0) {
								index = columnNum * 2;
							}
						}
						// 数字
						else if (type == SinoSearchFormConditionType.NUMBER) {
							int num = SinoSearchFormConditionType.getNumber(id);
							if ((num + 1) > columnNum * 2) {
								num = columnNum * 2 - 1;
							}

							if ((columnNum * 2 - index) < (num + 1)) {
								addRow(rows, conditionRows, columnNum);
								index = 0;
							}
							condition.setColspan(num);
							conditionRows.add(condition);
							index += (num + 1);
						}
						// 正常
						else {
							// 如果小于2换行
							if ((columnNum * 2 - index) < 2) {
								addRow(rows, conditionRows, columnNum);
								index = 0;
							}
							conditionRows.add(condition);
							index += 2;
						}

						if (index == columnNum * 2) {
							addRow(rows, conditionRows, columnNum);
							index = 0;
						}
					}
				}

				if (index > 0) {
					addRow(rows, conditionRows, columnNum);
				}
			}
		}
		this.conditionRows = rows;
	}

	private void addRow(List<SinoDefaultSearchConditionBean[]> rows,
			List<SinoDefaultSearchConditionBean> conditionRows, int columnNum) {
		rows.add(conditionRows
				.toArray(new SinoDefaultSearchConditionBean[columnNum]));
		conditionRows.clear();
	}

}